package com.chen.stopwatch.utils;

import java.util.Date;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.util.Log;

import com.chen.stopwatch.R;

public class Utils {
	public static final boolean ENABLE_LOG = false;

	public static String formatTime(long time) {
		int hour = (int) (time / 3600000);
		int min = (int) (time / 60000 - hour * 60);
		int sec = (int) (time / 1000 - hour * 3600 - min * 60);
		int milli = (int) (time - hour * 3600000 - min * 60000 - sec * 1000);

		return formatZero(2, hour) + ":" + formatZero(2, min) + ":"
				+ formatZero(2, sec) + ":" + formatZero(3, milli);
	}

	public static String formatZero(int median, int time) {
		if ((median == 2 && time < 10)
				|| (median == 3 && time < 100 && time >= 10)) {
			return "0" + time;
		} else if (median == 3 && time < 10) {
			return "00" + time;
		}
		return "" + time;
	}

	public static String formatStandardTime(long time) {
		try {
			Date date = new Date(time);
			java.text.DateFormat format = new java.text.SimpleDateFormat(
				"yyyy-MM-dd kk:mm:ss");
			return format.format(date);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	public static String formatStandardDateDay(long time) {
		try {
			Date date = new Date(time);
			java.text.DateFormat format = new java.text.SimpleDateFormat(
				"yyyy-MM-dd");
			return format.format(date);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	public static String formatStandardDateClock(long time) {
		try {
			Date date = new Date(time);
			java.text.DateFormat format = new java.text.SimpleDateFormat(
				"kk:mm:ss");
			return format.format(date);

		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	public static void logDebug(String tag, String msg) {
		if (ENABLE_LOG) {
			Log.d(tag, msg);
		}
	}

	public static void share(Context context, String time, String title) {
		if (null == context || null == title)
			return;
		try {
			Intent intent = new Intent(Intent.ACTION_SEND);
			intent.setType("text/plain");
			intent.putExtra(Intent.EXTRA_SUBJECT, context.getResources()
					.getString(R.string.share_title, time));
			intent.putExtra(Intent.EXTRA_TEXT, title);
			context.startActivity(intent);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static String getPkgVersionName(Context context, Class cls) {
		try {
			ComponentName comp = new ComponentName(context, cls);
			PackageInfo pinfo = context.getPackageManager().getPackageInfo(
				comp.getPackageName(), 0);

			return null == pinfo ? null : pinfo.versionName;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static long getDateDay(long time) {
		long day = time - time % 86400000;
		return day;
	}

	// public static void keepScreenOnOrOff(Activity activity, boolean on) {
	// if (null == activity)
	// return;
	// android.os.PowerManager manager = (PowerManager)
	// activity.getSystemService(Context.POWER_SERVICE);
	//
	// if (null == manager)
	// return;
	//
	// WakeLock lock = manager.newWakeLock(
	// PowerManager.FLAG_KEEP_SCREEN_ON , "My Lock");
	//
	// if (null == lock)
	// return;
	//
	// if (on) {
	// lock.acquire();
	// } else {
	// lock.setReferenceCounted(false);
	// lock.release();
	// }
	// }
}
